package com.tt.setting;

import android.content.Context;
import android.util.Log;

import com.tt.AppConfig;
import com.tt.listener.OnInitEngineListener;
import com.tt.util.AiUtil;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;


/**
 * Created by castiel on 2017/12/29.
 */

public class EngineSetting {

    private static final String TAG = "17kouyu";
    private String native_res_path2 = "%s/native.res";

    public File provisionFile;

    private String provisionPath;//开发证书文件路径(包含文件名)

    //cloud
    private String serverAddress = AppConfig.CLOUD_SERVER_ADDRESS;//服务器地址
    private String serverList = "";//获取serverList的地址，默认不配这一对键值
    private int connectTimeout = 10;//默认是10s，建立连接的超时时间
    private int serverTimeout = 60;//默认是60s，响应的超时时间
    //native
    private String nativeResourcePath;//离线资源本地路径

    //vad
    private boolean isVADEnabled = true;//是否打开VAD功能，默认关闭
    private int vadSeek = 60;//发音结束判断间隔，单位10ms，默认60，即600ms
    //sdkLog
    private boolean isSDKLogEnabled = true;//是否打开sdkLog，默认关闭

    private boolean isUseOnlineProvision;//是否使用在线证书，默认否
    private boolean isNeedUpdateOnlineProvision;//是否需要更新在线证书，默认否

    private String userId = "userId";

    private OnInitEngineListener onInitEngineListener;//初始化引擎监听

    private static EngineSetting mEngineSetting;

    private Context mContext;

    private EngineSetting(Context context){
        mContext = context;
    }

    public static EngineSetting getInstance(Context context) {
        return mEngineSetting == null ? mEngineSetting = new EngineSetting(context) : mEngineSetting;
    }

    //获取默认在线版设置实例
    public EngineSetting getDefaultCloudInstance(){
        EngineSetting setting = new EngineSetting(mContext);
        if(getDefaultProvisionFile() == null){
            Log.e(TAG, "provision file is null!");
        }else{
            //provision
            provisionFile = getDefaultProvisionFile();
            setting.setProvisionPath(provisionFile.getAbsolutePath())
                    //cloud
                    .setServerAddress(serverAddress)
                    .setConnectTimeout(connectTimeout)
                    .setServerTimeout(serverTimeout)
                    //VAD
                    .setVADEnabled(isVADEnabled)
                    .setVadSeek(vadSeek)
                    //sdkLog
                    .setSDKLogEnabled(isSDKLogEnabled)
                    //是否在线获取证书
                    .setUseOnlineProvision(isUseOnlineProvision)
                    //是否需要更新在线证书
                    .setNeedUpdateOnlineProvision(isNeedUpdateOnlineProvision);
        }
        return setting;
    }

    //获取默认离线版设置实例
    public EngineSetting getDefaultNativeInstance(){
        EngineSetting setting = new EngineSetting(mContext);
        if(getDefaultProvisionFile() == null){
            Log.e(TAG, "provision file is null!");
        }else{
            //provision
            provisionFile = getDefaultProvisionFile();
            setting.setProvisionPath(provisionFile.getAbsolutePath())
                    //离线资源
                    .setNativeResourcePath(getNativeResourcePath())
                    //VAD
                    .setVADEnabled(isVADEnabled)
                    .setVadSeek(vadSeek)
                    //sdkLog
                    .setSDKLogEnabled(isSDKLogEnabled)
                    //是否在线获取证书
                    .setUseOnlineProvision(isUseOnlineProvision)
                    //是否需要更新在线证书
                    .setNeedUpdateOnlineProvision(isNeedUpdateOnlineProvision);
        }
        return setting;
    }

    //获取项目中放置的证书文件
    public File getDefaultProvisionFile(){
        try {
            InputStream is = mContext.getAssets().open(AppConfig.PROVISION);
            File provisionFile = new File(AiUtil.externalFilesDir(mContext), AppConfig.PROVISION);
            AiUtil.writeToFile(provisionFile, is);
            is.close();
            return provisionFile;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    //获取开发证书文件路径
    private String getProvisionPath() {
        return provisionPath;
    }

    //设置开发证书文件路径
    private EngineSetting setProvisionPath(String provisionPath) {
        this.provisionPath = provisionPath;
        return this;
    }

    //获取服务器地址
    public String getServerAddress() {
        return serverAddress;
    }

    //设置服务器地址
    public EngineSetting setServerAddress(String serverAddress) {
        this.serverAddress = serverAddress;
        return this;
    }

    //获取serverList的地址
    public String getServerList() {
        return serverList;
    }

    //设置serverList的地址
    public EngineSetting setServerList(String serverList) {
        this.serverList = serverList;
        return this;
    }

    //获取建立连接的超时时间
    public int getConnectTimeout() {
        return connectTimeout;
    }

    //设置在线版建立连接的超时时间，默认是10s，离线版无效
    public EngineSetting setConnectTimeout(int connectTimeout) {
        this.connectTimeout = connectTimeout;
        return this;
    }

    //设置在线版响应的超时时间，默认是60s，离线版无效
    public EngineSetting setServerTimeout(int serverTimeout) {
        this.serverTimeout = serverTimeout;
        return this;
    }

    //获取响应的超时时间
    public int getServerTimeout() {
        return serverTimeout;
    }



    //获取离线资源本地路径
    public String getNativeResourcePath() {
        String resourceDir = new String(AiUtil.unzipFile(mContext, "native.zip").toString());
        nativeResourcePath = String.format(native_res_path2, resourceDir);
        return nativeResourcePath;
    }

    //设置离线资源本地路径
    private EngineSetting setNativeResourcePath(String nativeResourcePath) {
        this.nativeResourcePath = nativeResourcePath;
        return this;
    }

    //查看是否打开VAD
    public boolean isVADEnabled() {
        return isVADEnabled;
    }

    //设置是否打开VAD，VADEnabled true：启用；false：关闭
    public EngineSetting setVADEnabled(boolean VADEnabled) {
        isVADEnabled = VADEnabled;
        return this;
    }

    public int getVadSeek() {
        return vadSeek;
    }

    //发音结束判断间隔，单位10ms，默认60，即600ms
    public EngineSetting setVadSeek(int vadSeek) {
        this.vadSeek = vadSeek;
        return this;
    }

    //查看是否打开sdkLog
    public boolean isSDKLogEnabled() {
        return isSDKLogEnabled;
    }

    //设置是否打开sdkLog，SDKLogEnabled true：启用：false：关闭
    public EngineSetting setSDKLogEnabled(boolean SDKLogEnabled) {
        isSDKLogEnabled = SDKLogEnabled;
        return this;
    }

    public boolean isUseOnlineProvision() {
        return isUseOnlineProvision;
    }

    //设置是否使用在线证书，默认否
    public EngineSetting setUseOnlineProvision(boolean useOnlineProvision) {
        isUseOnlineProvision = useOnlineProvision;
        return this;
    }

    //是否需要更新在线证书，默认否
    public EngineSetting setNeedUpdateOnlineProvision(boolean needUpdateOnlineProvision) {
        isNeedUpdateOnlineProvision = needUpdateOnlineProvision;
        return this;
    }

    public boolean isNeedUpdateOnlineProvision() {
        return isNeedUpdateOnlineProvision;
    }

    public String getUserId() {
        return userId;
    }

    public EngineSetting setUserId(String userid) {
        if(userid != null && !"".equals(userid)){
            this.userId = userid;
        }
        return this;
    }

    public OnInitEngineListener getOnInitEngineListener() {
        return onInitEngineListener;
    }

    //初始化引擎监听
    public EngineSetting setOnInitEngineListener(OnInitEngineListener onInitEngineListener) {
        this.onInitEngineListener = onInitEngineListener;
        return this;
    }
}
